package jsyf.oa.service.wechartSec;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wechartSec.WechartUserEntity;
import jsyf.oa.facade.wechartSec.WechartUserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jsyf.oa.dao.wechartSec.WechartUserDao;
import jsyf.oa.service.wechat.service_company.HttpUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/8.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WechartUserService implements WechartUserFacade {

    @Autowired
    private WechartUserDao wechartUserDao;

    @Override
    public String save(String jsonData) {
        WechartUserEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), WechartUserEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = wechartUserDao.add(entity);
        } else {
            WechartUserEntity userEntity = (WechartUserEntity) wechartUserDao.getIBaseEntityById(entity.getId());
            userEntity.setName(entity.getName());
            userEntity.setTel(entity.getTel());
            userEntity.setWxId(entity.getWxId());
            userEntity.setWxAvatarUrl(entity.getWxAvatarUrl());
            userEntity.setWxNick(entity.getWxNick());
            rows = wechartUserDao.update(userEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public JSONObject getUserByWxId(String wxId) {
        List<JSONObject> users = wechartUserDao.getJSONListByCondition(" AND wx_id=?",new Object[]{wxId});
        if(users.size()>0){
            return users.get(0);
        }else{
            JSONObject map = new JSONObject();
            map.put("status", 0);
            map.put("msg", "用户不存在");
            return map;
        }
    }

    @Override
    public JSONObject getOpenId(String code){
        JSONObject map = new JSONObject();

        //登录凭证不能为空
        if (code == null || code.length() == 0) {
            map.put("status", 0);
            map.put("msg", "code 不能为空");
            return map;
        }

        //小程序唯一标识   (在微信小程序管理后台获取)
        String wxspAppid = "wx0d3f6cdacb3cedd2";
        //小程序的 app secret (在微信小程序管理后台获取)
        String wxspSecret = "1f08e259baad8f9a7e4b3962ed5d2413";
        //授权（必填）
        String grant_type = "authorization_code";


        //////////////// 1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid ////////////////
        //请求参数
        String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type=" + grant_type;
        //发送请求
        JSONObject jsonObject = HttpUtil.request("https://api.weixin.qq.com/sns/jscode2session", "GET", params);
        if (jsonObject!=null) {
            if (jsonObject.containsKey("errcode")) {
            }else {
                map = jsonObject;
            }
        }
        return map;
    }
    @Override
    public JSONObject getByMilkOpenId(String code){
        JSONObject map = new JSONObject();

        //登录凭证不能为空
        if (code == null || code.length() == 0) {
            map.put("status", 0);
            map.put("msg", "code 不能为空");
            return map;
        }

        //小程序唯一标识   (在微信小程序管理后台获取)
        String wxspAppid = "wx6f48361fe926a230";
        //小程序的 app secret (在微信小程序管理后台获取)
        String wxspSecret = "d869732f2a5400fbc7baf8d29b007c33";
        //授权（必填）
        String grant_type = "authorization_code";


        //////////////// 1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid ////////////////
        //请求参数
        String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type=" + grant_type;
        //发送请求
        JSONObject jsonObject = HttpUtil.request("https://api.weixin.qq.com/sns/jscode2session", "GET", params);
        if (jsonObject!=null) {
            if (jsonObject.containsKey("errcode")) {
            }else {
                map = jsonObject;
            }
        }
        return map;
    }
}